package com.inspur.iaic.analyze;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// cat access.log|grep "14/Apr/2018:14:54"| wc -l
public class AccessLog {
	static final String SQL = "insert into Access_Log(Id,Atime,Amethod,apath,Jsessionid,Issameuser,astatus) values(?,?,?,?,?,?,?)";
	static ExecutorService executor = Executors.newSingleThreadExecutor();
	private static Connection conn108() throws ClassNotFoundException, SQLException {
		String url = "jdbc:oracle:thin:@10.110.1.108:1521:orcl";
		String user = "gsls_test";
		String passwd = "111111";
		Connection conn = null;
		Class.forName("oracle.jdbc.driver.OracleDriver");
		conn = DriverManager.getConnection(url, user, passwd);
		return conn;
	}

	public static void main(String[] args) throws Exception {
		long startTime = System.currentTimeMillis();
		File log = new File("D:/work/project/gongshang/shandong/log/iaic/access.log");
		BufferedReader read = null;
		Connection conn = null;
		try {
			conn = conn108();
			final PreparedStatement ps = conn.prepareStatement(SQL);
			read = new BufferedReader(new FileReader(log));
			String line;
			int i = 0;
			while ((line = read.readLine()) != null) {
				if (isValidAccess(line)) {
					String[] words = line.split(" ");
					String time = words[3].substring(1);
					String method = words[5].substring(1);
					String path = words[6];
					String status = words[8];
					int sessionIndex = line.indexOf("JSESSIONID");
					int userIndex = line.indexOf("isSameUser");
					String jsessionid = null;
					String user = null;
					try {
						jsessionid = line.substring(sessionIndex + 11);
						jsessionid = jsessionid.substring(0, jsessionid.indexOf(";") < 0 ? jsessionid.indexOf("\"") : jsessionid.indexOf(";"));
						user = line.substring(userIndex + 11);
						user = user.substring(0, user.indexOf(";") < 0 ? user.indexOf("\"") : user.indexOf(";"));
					} catch (Exception e) {
						System.out.println(line);
						throw e;
					}
//					System.out.println(line);
//					System.out.println(time + "  " + method + "  " + path + "   " + status + "  " + jsessionid + "  " + user);
					ps.setInt(1, i);
					ps.setString(2, time);
					ps.setString(3, method);
					ps.setString(4, path.length() > 2000 ? path.substring(0, 2000) : path);
					ps.setString(5, jsessionid.length() > 300 ? jsessionid.substring(0, 300) : jsessionid);
					ps.setString(6, user);
					ps.setString(7, status);
					ps.addBatch();
					//696178 10000
					//692729
					if (i % 1000 == 0) {
						executor.execute(new Runnable() {
							@Override
							public void run() {
								try {
									ps.executeBatch();
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						});
					}
					i++;
				}

			}
		} finally {
			if (read != null) {
				read.close();
			}
			if (conn != null) {
				conn.close();
			}
		}
		executor.shutdown();
		System.out.println(System.currentTimeMillis() - startTime);
	}

	private static boolean isValidAccess(String line) {
		return line.indexOf("isSameUser") > 0 && line.indexOf("iaicweb") > 0 && (line.indexOf("jsp") > 0 || line.indexOf("command") > 0);
	}

}
